package GameState;

import java.awt.Graphics;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.rmi.RemoteException;
import javax.imageio.ImageIO;
import GameState.GamePiece;
import GameState.GameState;
import GameState.Projectiles.CannonBall;
import GameState.Projectiles.CannonBallRenderer;
import GameState.Ships.PirateShip;
import GameState.Ships.PirateShipRenderer;
import Server.Player;

public class GameStateRender {

	private final BufferedImage mSeaImage;
	private final static String SEA_IMAGE_PATH = "sea.jpg";
	private final double WIDTH;
	private final double HIEGHT;
	

	/* **********************************************************************************************************
	 * 													Renders to render everything							*
	 ********************************************************************************************************** */
	private PirateShipRenderer pRender;
	private CannonBallRenderer cRender;

	public GameStateRender(int width, int hieght ) throws IOException
	{
		WIDTH = width;
		HIEGHT = hieght;
		mSeaImage = ImageIO.read( new File(SEA_IMAGE_PATH));
		pRender = new PirateShipRenderer();
		cRender = new CannonBallRenderer();
	}

	public BufferedImage render(GameState gameState, Player currentPlayer)
	{
		return renderBlock(gameState, currentPlayer);
	}

	
	private BufferedImage renderBlock(GameState gameState, Player currentPlayer){

		
		Point playerPosition = gameState.getShip(currentPlayer).getPosition();
		BufferedImage blockImage  = new BufferedImage(2000,2000, BufferedImage.TYPE_INT_ARGB);
		Graphics blockGraphics = blockImage.getGraphics();

		drawPlayerVeiwImage(blockGraphics, mSeaImage, playerPosition);

		for (GamePiece gp : gameState){
			
			switch (gp.getRenderType()){
			
			case SHIP:
				
				drawPlayerVeiwImage(blockGraphics, pRender.renderShip((PirateShip) gp), playerPosition);
				
				break;
				
			case CANNON_BALL:
							
				drawPlayerVeiwImage(blockGraphics, cRender.render((CannonBall) gp), playerPosition);
				break;
						
			}
						
			
		}
		
		
//		/*
//		 ************************************************************************************************
//		 * 									Draws The Ships on the screen								*
//		 ************************************************************************************************
//		 */
//		//*
//		//draws all the ships to the screen
//		for(int count = 0 ; count < myShips.size(); count++){
//
//			//draws an individual ship from the players point of view			
//				blockGraphics,
//					renderShip(myShips.get(count)),
//					myShips.get(count).getPosition());
//
//		}
//
//		/*
//		 ************************************************************************************************
//		 * 								Draws The Cannon Balls on the screen							*
//		 ************************************************************************************************
//		 */
//		for(int count = 0 ; count < myBalls.size(); count++){
//
//			drawPlayerVeiwImage(blockGraphics, mBallImage, myBalls.get(count).getPosition());
//
//		}


		return blockImage;



	}
	
	
	/**
	 * 
	 * draws an Image on to the screen from the player point of view.
	 *  
	 * @param g graphics that the image is drawn on.
	 * @param b image which is drawn on.
	 * @param x X position which that the image is drawn on.
	 * @param y Y position which that the image is drawn on.
	 * @throws RemoteException 
	 */
	private void drawPlayerVeiwImage(final Graphics g, final BufferedImage b, final Point position ){

		int playerVeiwX = (int) (position.x + WIDTH /2 - position.getX()) ;
		int playerVeiwY = (int) (position.y + HIEGHT /2 - position.getY());

		drawCenteredImage(g, b, playerVeiwX  , playerVeiwY, 2000);

	}

	/**
	 *
	 * Draws an image centered.
	 * 
	 * @param g graphics that the image is drawn on.
	 * @param b image which is drawn on.
	 * @param x X position which that the image is drawn on.
	 * @param y Y position which that the image is drawn on.
	 */
	private void drawCenteredImage(final Graphics g, final BufferedImage b, final int x , final int y, int mapUnit ){

		/*
	 	 _____ _____ _____
		|	  |		|	  |
		| TL  |	TM	| TR  |
		|_____|_____|_____|
		|	  |		|     |
		| CL  |	CM	| CR  |
		|_____|_____|_____|
		|	  |		|     |
		| BL  |	BM	| BR  |
		|_____|_____|_____|
		 */
		//TODO abstact to map

		//centre mid block
		g.drawImage(b, x - b.getWidth()/2, y - b.getHeight()/2, null);

		
		//
		// Top blocks
		//

		//top left block
		g.drawImage(b, x - b.getWidth()/2  - mapUnit, y - b.getHeight()/2 - mapUnit, null);

		//top mid block
		g.drawImage(b, x - b.getWidth()/2, y - b.getHeight()/2 - mapUnit, null);

		//top right block
		g.drawImage(b, x - b.getWidth()/2 + mapUnit, y - b.getHeight()/2 - mapUnit, null);


		//
		// Centre blocks
		//

		//centre left block
		g.drawImage(b, x - b.getWidth()/2  - mapUnit, y - b.getHeight()/2, null);

		//centre right block
		g.drawImage(b, x - b.getWidth()/2 + mapUnit, y - b.getHeight()/2, null);

		
		//
		// Bottom Block
		//

		//Bottom left block
		g.drawImage(b, x - b.getWidth()/2 - mapUnit, y - b.getHeight()/2 + mapUnit, null);

		//Bottom mid block
		g.drawImage(b, x - b.getWidth()/2, y - b.getHeight()/2 + mapUnit, null);

		//Bottom right block
		g.drawImage(b, x - b.getWidth()/2 + mapUnit, y - b.getHeight()/2 + mapUnit, null);

	}
	
	
}
